package net.contrend.system.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.contrend.system.dao.UserRoleDao;
import net.contrend.util.BaseAction;
import net.contrend.util.StringUtils;

public class UserRoleAction extends BaseAction {
    
    private UserRoleDao userRoleDao;
    
    private void initDao() {
        if (null == this.userRoleDao) {
            this.userRoleDao = (UserRoleDao) this.getBean("userRoleDao");
        }
    }
    
    public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        this.initDao();
        
        String userId = request.getParameter("userId");
        String roleIds = request.getParameter("roleIds");
        if (StringUtils.isNotBlank(userId) && null != roleIds) {
            String[] _roleIds = roleIds.replaceAll("\\s", "").split(",");
            this.userRoleDao.insert(userId, _roleIds);
            response.getWriter().write(String.valueOf(1));
        } else {
            response.getWriter().write(String.valueOf(0));
        }
        
        return null;
    }
    
}
